<?php


namespace King\Service;


use King\Entity\Poker\Poker;

class PokerService
{

    public static $pokerList = ['3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A', '2',];

    public static $decorPokerList = ['spade', 'club', 'heart', 'diamonds'];

    public function make(): array
    {
        $result = [];
        foreach (self::$pokerList as $number) {
            foreach (self::$decorPokerList as $decor) {
                $poker = new Poker();
                $poker->name = $number;
                $poker->color = $decor;
                array_push($result, $poker);
            }
        }
        array_push($result, $this->joker());
        array_push($result, $this->trump());
        return $result;
    }

    protected function joker(): Poker
    {
        $poker = new Poker();
        $poker->name = 'joker';
        return $poker;
    }

    protected function trump(): Poker
    {
        $poker = new Poker();
        $poker->name = 'trump';
        return $poker;
    }

    public function deal(): array
    {
        $maxPeople = 3;
        $handCardMax = 3;
        $service = new PokerService();
        $list = $service->make();
        $mainCardIndex = array_rand($list, 1);
        $mainCard = $list[$mainCardIndex];
        shuffle($list);
        $handCard = [];
        for ($i = 0; $i < $handCardMax; $i++) {
            array_push($handCard, array_shift($list));
        }
        $personCardList = [];
        for ($i = 0; $i < $maxPeople; $i++) {
            $personCardList[$i] = [];
        }
        $count = count($list);
        $remainder = $count % $maxPeople;
        $divisor = $count / $maxPeople;
        if ($remainder > 0) {
            exit('暂不考虑');
        } else {
            for ($i = 0; $i < $divisor; $i++) {
                for ($j = 0; $j < $maxPeople; $j++) {
                    array_push($personCardList[$j], array_shift($list));
                }
            }
        }

        return [
            'person' => $personCardList,
            'hand' => $handCard,
            'mainCard' => $mainCard,
        ];
    }

}